classdef batchperceptron < handle
    properties
       grammatrix;
       alpha;
       S;
       Y;
       ker;
    end
    methods
        function obj = batchperceptron(S, Y, ker)
            obj.grammatrix = ker.dot(S,S);
            obj.alpha = zeros(size(S,1),1);
            obj.S = S;
            obj.Y = Y;
            obj.ker = ker;
        end
        %TODO Review batch
        function mistakes = train(obj)
            mistakes = 0;
            for i=1:size(obj.alpha,1)
                yi = obj.Y(i);
                acc = sum(obj.alpha.* obj.Y .* obj.grammatrix(:,i));
                if(yi*acc <= 0)
                    obj.alpha(i) = obj.alpha(i) + 1;
                    mistakes = mistakes + 1;
                end                
            end
        end
        
        function classes = predict(obj, xi)
            len = size(xi,1);
            classes = zeros(len ,1);
            for i = 1:len  
                classes(i) = sum(obj.alpha.* obj.Y .* obj.ker.dot(obj.S, xi(i,:))) > 0;
                 if class(i) == 0
                     classes(i) = -1;
                 end
            end
        end
        
        function vector = getvector(obj)
            vector = sum(obj.alpha*ones(1,size(obj.S,2)) .* obj.S .* (obj.Y* ones(1,size(obj.S,2))));
        end
    end    
end